#include "stdafx.h"
#include "MyEventReceiver.h"

extern float cellsize;
extern float cellheight;
extern float agentheight;
extern float agentmaxclimb;
extern float agentradius;
extern float maxedgelen;
extern float minregionsize;
extern float mergeregionsize;
extern int maxvertsperpoly;
extern float detailsampledist;
extern float detailsamplemaxerror;

extern void compileNavmesh();
extern scene::ICameraSceneNode* quakecamera;
extern scene::ICameraSceneNode* camera;

extern gui::IGUIEditBox* edit_cellsize;
extern gui::IGUIEditBox* edit_cellheight;
extern gui::IGUIEditBox* edit_agentheight;
extern gui::IGUIEditBox* edit_agentmaxclimb;
extern gui::IGUIEditBox* edit_agentradius;
extern gui::IGUIEditBox* edit_maxedgelen;
extern gui::IGUIEditBox* edit_minregionsize;
extern gui::IGUIEditBox* edit_mergeregionsize;
extern gui::IGUIEditBox* edit_maxvertsperpoly;
extern gui::IGUIEditBox* edit_detailsampledist;
extern gui::IGUIEditBox* edit_detailsamplemaxerror;

MyEventReceiver::MyEventReceiver(SAppContext& ctx)
:	context(ctx)
{
}

bool MyEventReceiver::OnEvent(const SEvent& event)
{
	if (event.EventType == EET_GUI_EVENT)
	{
		s32 id = event.GUIEvent.Caller->getID();
		switch (event.GUIEvent.EventType) 
		{
		case EGET_BUTTON_CLICKED:
			switch (id)
			{
			case GUI_ID_COMPILE:
				cellsize = (float)_wtof(edit_cellsize->getText());
				cellheight = (float)_wtof(edit_cellheight->getText());
				agentheight = (float)_wtof(edit_agentheight->getText());
				agentmaxclimb = (float)_wtof(edit_agentmaxclimb->getText());
				agentradius = (float)_wtof(edit_agentradius->getText());
				maxedgelen = (float)_wtof(edit_maxedgelen->getText());
				minregionsize = (float)_wtof(edit_minregionsize->getText());
				mergeregionsize = (float)_wtof(edit_mergeregionsize->getText());
				maxvertsperpoly = _wtoi(edit_maxvertsperpoly->getText());
				detailsampledist = (float)_wtof(edit_detailsampledist->getText());
				detailsamplemaxerror = (float)_wtof(edit_detailsamplemaxerror->getText());
				compileNavmesh();
				return true;
			default:
				return false;
			}
		default:
			return false;
		}
	}
	if (event.EventType == EET_MOUSE_INPUT_EVENT)
	{
		if (event.MouseInput.isRightPressed())
		{
			//scene::ICameraSceneNode* cam = context.device->getSceneManager()->getActiveCamera();
			if (!context.quakecam) 
			{
				context.quakecam = true;
				context.device->getCursorControl()->setVisible(false);
				context.device->getSceneManager()->setActiveCamera(quakecamera);
			}
			else
			{
				context.quakecam = false;
				context.device->getCursorControl()->setVisible(true);
				camera->setPosition(quakecamera->getPosition());
				camera->setTarget(quakecamera->getTarget());
				context.device->getSceneManager()->setActiveCamera(camera);
			}			
		}
	}
	return false;
}